<!-- src/views/user/RoleAdd.vue -->
<script setup>
import { ref, reactive } from 'vue';
import { useRoleStore } from '@/stores/roleStore.js';

const roleStore = useRoleStore();
const dialogVisible = ref(false);

const role = reactive({
  name: '',
  code: '',
  description: '',
  dataScope: null
});

const roleForm = ref(null);
const submitForm = () => {
  roleForm.value?.validate().then(() => {
    console.log("校验通过", role);
    console.log("role", role);

    roleStore.addRole({
      ...role
    }).then(() => {
      close();
    });

  }).catch(() => {
    console.log("校验失败");
  });
};

const show = () => {
  // 重置表单数据
  Object.assign(role, {
    name: '',
    code: '',
    description: '',
    dataScope: null
  });
  dialogVisible.value = true;
};

const close = () => {
  dialogVisible.value = false;
};

// 对外暴露show方法
defineExpose({
  show
});
</script>

<template>
  <el-dialog
      title="添加角色"
      v-model="dialogVisible"
      width="50%"
  >
    <el-form :model="role" label-width="100px" ref="roleForm">
      <el-form-item label="角色名称" prop="name" :rules="[{ required: true, message: '请输入角色名称', trigger: 'blur' }]">
        <el-input v-model="role.name" />
      </el-form-item>
      <el-form-item label="角色编码" prop="code" :rules="[{ required: true, message: '请输入角色编码', trigger: 'blur' }]">
        <el-input v-model="role.code" />
      </el-form-item>
      <el-form-item label="数据权限范围" prop="dataScope" :rules="[{ required: true, message: '请选择数据权限范围', trigger: 'change' }]">
        <el-select v-model="role.dataScope" placeholder="请选择数据权限范围">
          <el-option label="全部" :value="1"></el-option>
          <el-option label="本部门" :value="2"></el-option>
          <el-option label="自定义" :value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="角色描述">
        <el-input v-model="role.description" type="textarea" />
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="close">取消</el-button>
      <el-button type="primary" @click="submitForm">确定</el-button>
    </template>
  </el-dialog>
</template>

<style scoped>

</style>
